<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="使用停车场" prop="parkId">
        <el-select v-model="queryParams.parkId" placeholder="请选择停车场" clearable>
          <el-option
            v-for="dict in parkingInfoList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="上架" :value="0"/>
          <el-option label="下架" :value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['merchant:platformCoupon:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['merchant:platformCoupon:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:platformCoupon:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:platformCoupon:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="platformCouponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="使用停车场" width="200" align="center" prop="parkName" />
      <el-table-column label="套餐名称" width="200" align="center" prop="couponName" />
      <el-table-column label="面值总额" align="center" prop="faceValue" />
      <el-table-column label="实际售价" align="center" prop="actualPrice" />
      <el-table-column label="有效期类型" align="center" prop="validType">
        <template slot-scope="scope">
          <span>{{ scope.row.validType==1?"固定时间":"领取后天数" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生效开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生效结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期天数" align="center" prop="validDays" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status==0">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.status==1">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="使用限制" align="center" prop="useRule">
          <template slot-scope="scope">
            {{ scope.row.useRule?('满'+scope.row.useRule+'元可用'):'' }}
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:platformCoupon:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:platformCoupon:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券套餐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="指定商户" prop="merchantId">
          <el-select v-model="form.merchantId" placeholder="请选择指定商户" clearable>
            <el-option
              v-for="dict in merchantList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="使用停车场" prop="parkId">
          <el-select v-model="form.parkId" placeholder="请选择使用停车场" clearable>
            <el-option
              v-for="dict in parkingInfoList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="套餐名称" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="面值总额" prop="faceValue">
          <el-input v-model="form.faceValue" placeholder="请输入面值总额" />
        </el-form-item>
        <el-form-item label="实际售价" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入实际售价" />
        </el-form-item>
        <el-form-item label="有效期类型" prop="validType">
          <el-select v-model="form.validType" placeholder="请选择有效期类型" clearable>
            <el-option label="固定时间" :value="1"/>
            <el-option label="领取后天数" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="生效开始时间" prop="startTime" v-if="form.validType==1">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择生效开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生效结束时间" prop="endTime" v-if="form.validType==1">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择生效结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期天数" prop="validDays" v-if="form.validType==2">
          <el-input type="number" v-model="form.validDays" placeholder="请输入有效期天数" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="使用限制" prop="useRule">
          <el-input v-model="form.useRule" placeholder="满多少元可使用" />
        </el-form-item>
        <el-form-item label="售卖说明" prop="couponDesc">
          <el-input v-model="form.couponDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlatformCoupon, getPlatformCoupon, delPlatformCoupon, addPlatformCoupon, updatePlatformCoupon } from "@/api/merchant/platformCoupon";
import { selectParkingInfoList } from "@/api/parking/selectList";
import { selectMerchantList } from "@/api/merchant/selectList";

export default {
  name: "PlatformCoupon",
  dicts: ['sys_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券套餐表格数据
      platformCouponList: [],
      parkingInfoList: [],
      merchantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkId: null,
        couponName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        couponName: [
          { required: true, message: "套餐名称不能为空", trigger: "blur" }
        ],
        faceValue: [
          { required: true, message: "面值总额不能为空", trigger: "blur" }
        ],
        actualPrice: [
          { required: true, message: "实际售价不能为空", trigger: "blur" }
        ],
        validType: [
          { required: true, message: "请选择有效期类型", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParkingInfoList();
    this.getMerchantList();
  },
  methods: {
    /** 查询优惠券套餐列表 */
    getList() {
      this.loading = true;
      listPlatformCoupon(this.queryParams).then(response => {
        this.platformCouponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkingInfoList() {
      selectParkingInfoList().then(response => {
        this.parkingInfoList = response.data;
      });
    },
    getMerchantList() {
      selectMerchantList().then(response => {
        this.merchantList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        merchantId: null,
        parkId: null,
        parkName: null,
        couponName: null,
        faceValue: null,
        actualPrice: null,
        discountRate: null,
        validType: null,
        startTime: null,
        endTime: null,
        validDays: null,
        status: 0,
        useRule: null,
        couponDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券套餐";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlatformCoupon(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券套餐";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.parkName = this.parkingInfoList.find(item => item.value === this.form.parkId).label;
          if (this.form.id != null) {
            updatePlatformCoupon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlatformCoupon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除优惠券套餐编号为"' + ids + '"的数据项？').then(function() {
        return delPlatformCoupon(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('merchant/platformCoupon/export', {
        ...this.queryParams
      }, `platformCoupon_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
